<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="userName" size="small">
            <el-input v-model="searchMap.userName" placeholder="岗位编码"></el-input>
          </el-form-item>
          <el-form-item prop="phonenumber" size="small">
            <el-input v-model="searchMap.phonenumber" placeholder="岗位名称"></el-input>
          </el-form-item>
          <el-form-item prop="status" style="width: 220px" size="small">
            <el-select v-model="searchMap.status" placeholder="状态">
              <!-- <el-option v-for="option in PayOption" :key="option.type" :label="option.name" :value="option.type"></el-option> -->
            </el-select>
          </el-form-item>
          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit">
          <el-form-item class="flex">
            <el-button @click="handleAdd()" class="btn_primary" icon="el-icon-plus">
              新增
            </el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table :data="postList" stripe class="per_table" style="width: 100%">
          <el-table-column prop="postId" label="岗位编号"></el-table-column>
          <el-table-column prop="postCode" label="岗位编码"></el-table-column>
          <el-table-column prop="postName" label="岗位名称"></el-table-column>
          <el-table-column prop="postSort" label="权限标识"></el-table-column>
          <el-table-column key="status" label="状态">
            <!-- <template slot-scope="scope">
               <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" /> 
            </template> -->
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop="createTime">
            <template slot-scope="scope">
              <div class="button-group">
                <span type="text" class="tag_primary" size="small" @click="handleEdit(scope.row)">编辑</span>
                <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 表格模块over -->

      <!-- 分页 -->
      <pagination v-show="total > 0" :total="total"></pagination>
      <!-- 分页over -->
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisiblePost" append-to-body>
      <el-form ref="form" :model="formModel" :rules="rules" label-width="80px" size="small">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="formModel.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="formModel.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort" size="small">
          <el-input-number v-model="formModel.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status" size="small">
          <el-radio-group v-model="formModel.status">
            <!-- <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio> -->
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark" size="small">
          <el-input v-model="formModel.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblePost = false" class="btn_cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" class="btn_primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, addPost, updatePost, getPost, delPost } from '@/api'
export default {
  name: 'role',
  data() {
    return {
      // 弹框标题
      title: '标题',
      // 弹框
      dialogVisiblePost: false,
      // 搜索条件
      searchMap: {},
      // 岗位列表
      postList: [],
      // 总条数
      total: 0,
      // 表单数据
      formModel: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  computed: {
    pageTitle() {
      return '岗位管理'
    }
  },
  methods: {
    getList() {
      listPost(this.searchMap).then(response => {
        const resp = response
        this.postList = resp.rows
        this.total = resp.total
      })
    },
    // 表单重置
    reset() {
      this.formModel = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 点击新增按钮触发
    handleAdd() {
      this.reset();
      console.log(11)
      this.dialogVisiblePost = true
      this.title = '添加岗位'
    },
    handleDelete(row) {
      this.$confirm('是否删除该岗位', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPost(row.postId).then(response => {
          console.log(response)
          if (response.code === 200) {
            this.$message.success("删除成功");
            this.getList();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleEdit(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.formModel = response.data;
        this.dialogVisiblePost = true;
        this.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formModel.postId != undefined) {
            updatePost(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message.success("新增成功");
                this.dialogVisiblePost = false;
                this.getList();
              }
            });
          }
        }
      });
    },
  },
}
</script>

<style lang="less" scoped>
.list_container {
  flex-direction: column;
  background: url("@/assets/images/home/content.png") no-repeat;
  background-size: cover;

  .content_container {
    padding: 15px 20px;
    overflow-y: scroll;
  }

  .table_container {
    margin-bottom: 25px;
  }

  .pagination_container {
    display: flex;
    justify-content: space-between;
    height: 32px;
  }
}</style>